package com.fz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.entity.Cart;
import com.fz.mapper.CartMapper;
import com.fz.service.CartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 存储购物车中商品信息 服务实现类
 * </p>
 *
 * @author hyr
 * @since 2025-06-18
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {
    @Override
    public IPage<Cart> getByUserId(Long userId, Integer pageNo, Integer pageSize) {

        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return this.page((new Page<>(pageNo, pageSize)), wrapper);

    }

    @Override
    public Boolean add( Long goodsId,Long userId,Integer count) {
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("goods_id", goodsId);
        wrapper.eq("store_id", 1l);
        Cart cart0 = this.getOne(wrapper);
        if (cart0!=null){
            return false;
        }
        LocalDateTime now = LocalDateTime.now();
        Cart cart = new Cart( goodsId,userId, count,now);

        return this.save(cart);
    }

    @Override
    public Boolean update(Long id, Integer count) {
        QueryWrapper<Cart> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Cart cart0 = this.getOne(wrapper);
        if (cart0!=null){
            cart0.setCount(count);
            System.out.println(cart0.getCount());
            return this.updateById(cart0);
        }
        return false;
    }
}
